package com.ruoyi.framework.weChat.config;

import com.ruoyi.framework.weChat.api.WeChat;
import com.ruoyi.framework.weChat.connect.WeChatConnectionFactory;
import com.ruoyi.framework.weChat.properties.BaseSecurityProperties;
import com.ruoyi.framework.weChat.properties.WeChatProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.UsersConnectionRepository;

/**
 * @Description TODO
 * @Author h21448-hxtq
 * @Date 2020-04-01 14 : 33
 * @Version 1.0
 */
@Configuration
@EnableSocial
@ConditionalOnProperty(prefix = "baseframe.security.social.wechat", name = "app-id")
public class WeChatAuthConfig extends SocialAutoConfigurerAdapter {
    @Autowired
    private BaseSecurityProperties baseSecurityProperties;

    @Override
    public void addConnectionFactories(ConnectionFactoryConfigurer configurer, Environment environment) {
        configurer.addConnectionFactory(createConnectionFactory());
    }

    @Override
    protected ConnectionFactory<?> createConnectionFactory() {
        WeChatProperties weiXin = baseSecurityProperties.getSocial().getWeChat();
        String providerId = weiXin.getProviderId();
        String appId = weiXin.getAppId();
        String appSecret = weiXin.getAppSecret();
        return new WeChatConnectionFactory(providerId, appId, appSecret);
    }

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        return null;
    }

}
